﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using WpfApp.Models;
using WpfApp.Views;

namespace WpfApp.ViewModels
{
    internal class WordBookViewModel: NotificationObject
    {
        private string? user;
        public string? bookName { get; set; }
        private Word? currentWord;
        private int currentIndex = 0;
        private Word? displayedWord;

        public Word? DisplayedWord
        {
            get { return displayedWord; }
            set
            {
                displayedWord = value;
                this.RaisePropertyChanged("DisplayedWord");
            }
        }
        private int num;

        public int Num
        {
            get { return num; }
            set
            {
                num = value;
                this.RaisePropertyChanged("Num");
            }
        }



        public ObservableCollection<Word> learnWords { get; set; }
        public ObservableCollection<Word> unlearnWords { get; set; }
        public ObservableCollection<Word> displayedWords { get; set; }
        public ObservableCollection<Word> DisplayedWords { get => displayedWords;
            set
            {
                displayedWords = value;
                this.RaisePropertyChanged("DisplayedWords");
            }
        }

        private string visible;

        public string Visible
        {
            get { return visible; }
            set
            {
                visible = value;
                this.RaisePropertyChanged("Visible");
            }
        }
        private string visible_1;

        public string Visible_1
        {
            get { return visible_1; }
            set
            {
                visible_1 = value;
                this.RaisePropertyChanged("Visible_1");
            }
        }
        private string visible_2;

        public string Visible_2
        {
            get { return visible_2; }
            set
            {
                visible_2 = value;
                this.RaisePropertyChanged("Visible_2");
            }
        }





        public Word CurrentWord
        {
            get => currentWord;
            set
            {
                currentWord = value;
                this.RaisePropertyChanged("CurrentWord");
            }
        }
        public DelegateCommand? ShowLearnWordsCommand { get; set; }
        public DelegateCommand? ShowUnlearnWordsCommand { get; set; }
        public DelegateCommand? RememberCommand { get; set; }
        public DelegateCommand? KnowCommand { get; set; }
        public DelegateCommand? FuzzyCommand { get; set; }
        public DelegateCommand? UnKownCommand { get; set; }
        public DelegateCommand? DisplayCommand { get; set; }
        public DelegateCommand? MeaningCommand { get; set; }

        private void NextWord()
        {
            if (currentIndex < unlearnWords.Count)
            {
                CurrentWord = unlearnWords[currentIndex];
            }
            else
            {
                // Handle case where there are no more words
                CurrentWord = null;
            }
        }

        private void RememberWord(object obj)
        {
            unlearnWords[currentIndex].Score = 3;
            MainWindow.data.ChangeWord(MainMenu.userName, bookName, unlearnWords[currentIndex]);
            if (CurrentWord != null)
            {
                learnWords.Insert(0, CurrentWord);
                unlearnWords.Remove(CurrentWord);
                currentIndex = currentIndex >= unlearnWords.Count ? 0 : currentIndex; // Reset index if it exceeds the new count
                NextWord();
            }
        }
        private void KnowWord(object obj)
        {
            if (CurrentWord != null)
            {
                unlearnWords[currentIndex].Score += 1;
                MainWindow.data.ChangeWord(MainMenu.userName, bookName, unlearnWords[currentIndex]);
                if (unlearnWords[currentIndex].Score>=3)
                {
                    learnWords.Insert(0,CurrentWord);
                    unlearnWords.Remove(CurrentWord);
                }
                
            }
            
            currentIndex = (currentIndex + 1) % unlearnWords.Count;
            NextWord();
        }
        private void UnKnowWord(object obj)
        {
            currentIndex = (currentIndex + 1) % unlearnWords.Count;
            NextWord();
        }

        private void FuzzyWord(object obj)
        {
            unlearnWords[currentIndex].Score = 0;
            MainWindow.data.ChangeWord(MainMenu.userName, bookName, unlearnWords[currentIndex]);
            currentIndex = (currentIndex + 1) % unlearnWords.Count;
            NextWord();

        }

        public void showLearnWords(object obj)
        {
            DisplayedWords = learnWords;
            Num = learnWords.Count;
            Visible_1 = "Visible";
            Visible_2 = "Hidden";
        }
        public void showUnlearnWords(object obj)
        {
            DisplayedWords = unlearnWords;
            Num = unlearnWords.Count;
            Visible_1 = "Visible";
            Visible_2 = "Hidden";
        }

        public void display(object obj)
        {
            DisplayedWord = (Word)obj;
        }
        public void meaning(object obj)
        {
            if (Visible == "Hidden") Visible = "Visible";
            else Visible = "Hidden";
        }

        public WordBookViewModel(Dictionary wordBook)
        {
            bookName = wordBook.Title;
            learnWords = new ObservableCollection<Word>(MainWindow.data.GetLearnedWords(MainMenu.userName, bookName));
            unlearnWords = new ObservableCollection<Word>(MainWindow.data.GetUnLearnedWords(MainMenu.userName, bookName));
            if(unlearnWords.Count > 0) { CurrentWord= unlearnWords[0]; }
            Visible = "Visible";
            Visible_1 = "Visible";
            Visible_2 = "Hidden";
            DisplayedWords = learnWords;
            Num = learnWords.Count;
            ShowLearnWordsCommand = new DelegateCommand();
            ShowLearnWordsCommand.ExecuteAction += new Action<object>(this.showLearnWords);
            ShowUnlearnWordsCommand = new DelegateCommand();
            ShowUnlearnWordsCommand.ExecuteAction += new Action<object>(this.showUnlearnWords);
            KnowCommand = new DelegateCommand();
            KnowCommand.ExecuteAction += new Action<object>(this.KnowWord);
            RememberCommand = new DelegateCommand();
            RememberCommand.ExecuteAction += new Action<object>(this.RememberWord);
            UnKownCommand = new DelegateCommand();
            UnKownCommand.ExecuteAction += new Action<object>(this.UnKnowWord);
            FuzzyCommand = new DelegateCommand();
            FuzzyCommand.ExecuteAction += new Action<object>(this.FuzzyWord);
            DisplayCommand = new DelegateCommand();
            DisplayCommand.ExecuteAction += new Action<object>(this.display);
            MeaningCommand = new DelegateCommand();
            MeaningCommand.ExecuteAction += new Action<object>(this.meaning);
        }

        
    }
}
